package leet;

import common.TreeNode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author fance
 * @date 18-4-17 下午4:29
 */
public class  TreeSum {

    /**
     * 层序遍历
     * @param root
     * @return
     */
    public ArrayList<ArrayList<Integer>> levelPrint(TreeNode root) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int curSize = queue.size();
            ArrayList<Integer> tmp = new ArrayList<>();
            for (int i = 0; i < curSize; i++) {
                TreeNode cur = queue.poll();
                tmp.add(cur.val);
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            res.add(tmp);
        }
        return res;
    }


    /**
     * 非递归遍历  前  中  后
     * @param root
     * @return
     */
    public ArrayList<Integer> preOrderUnRecur(TreeNode root) {
        ArrayList<Integer> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        LinkedList<TreeNode> stack = new LinkedList<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode cur = stack.pop();
            res.add(cur.val);
            if (cur.right != null) {
                stack.push(cur.right);
            }
            if (cur.left != null) {
                stack.push(cur.left);
            }
        }
        return res;
    }


    public ArrayList<Integer> inOrderUnRecur(TreeNode root) {
        ArrayList<Integer> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        LinkedList<TreeNode> stack = new LinkedList<>();
        while (!stack.isEmpty() || root != null) {
            if (root != null) {
                stack.push(root);
                root = root.left;
            } else {
                TreeNode cur = stack.pop();
                res.add(cur.val);
                root = cur.right;
            }
        }
        return res;
    }

    public ArrayList<Integer> postOrderUnRecur(TreeNode root) {
        ArrayList<Integer> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        LinkedList<TreeNode> stack1 = new LinkedList<>();
        LinkedList<TreeNode> stack2 = new LinkedList<>();
        stack1.push(root);
        while (stack1.isEmpty() == false) {
            root = stack1.pop();
            stack2.push(root);

            if (root.left != null) {
                stack1.push(root.left);
            }
            if (root.right != null) {
                stack1.push(root.right);
            }
        }
        while (stack2.isEmpty() == false) {
            res.add(stack2.pop().val);
        }
        return res;
    }

    /**
     * 104. 二叉树的最大深度
     * @param root
     * @return
     */
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return Math.max(maxDepth(root.left),maxDepth(root.right)) + 1;
    }


    /**
     * 105. 从前序与中序遍历序列构造二叉树
     * @param preorder
     * @param inorder
     * @return
     */
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if (preorder == null || preorder.length < 1) {
            return null;
        }
        return buildTreeCore(preorder,0,preorder.length - 1,inorder,0,inorder.length - 1);
    }
    private TreeNode buildTreeCore(int[] preorder,int preSt,int preEnd, int[] inorder,int inSt,int inEnd) {
        if (preSt > preEnd || inSt > inEnd || preEnd >= preorder.length || inEnd >= inorder.length) {
            return null;
        }
        int rootVal = preorder[preSt];
        int rootIndexInOrder = 0;
        for (int i = inSt; i <= inEnd; i++) {
            if (inorder[i] == rootVal) {
                rootIndexInOrder = i;
            }
        }
        int offset = rootIndexInOrder - inSt;
        TreeNode root = new TreeNode(rootVal);
        root.left = buildTreeCore(preorder,preSt + 1,preSt + offset,inorder,inSt,rootIndexInOrder - 1);
        root.right = buildTreeCore(preorder,preSt + offset + 1,preEnd,inorder,rootIndexInOrder + 1,inEnd);
        return root;
    }

    /**
     * 103. Binary Tree Zigzag Level Order Traversal
     * @param root
     * @return
     */
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (queue.isEmpty() == false) {
            int curSize = queue.size();
            List<Integer> tmp = new ArrayList<>();
            for (int i = 0; i < curSize; i++) {
                TreeNode cur = queue.poll();
                tmp.add(cur.val);
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            res.add(tmp);
        }
        for (int i = 0; i < res.size(); i++) {
            if (i % 2 != 0) {
                Collections.reverse(res.get(i));
            }
        }
        return res;
    }


}
